package org.shj.basic

object PartialFuncDemo {
  def main(args: Array[String]): Unit = {
    val sample = 1 to 10
    val isEven: PartialFunction[Int, String] = {
      case x if x % 2 == 0 => x+" is even"
    }
    
    //println(isEven(3)) //此句会抛错
  
    // the method collect can use isDefinedAt to select which members to collect
    val evenNumbers = sample collect isEven
    evenNumbers.foreach(println)
    
    println("==================")
  
    val isOdd: PartialFunction[Int, String] = {
      case x if x % 2 == 1 => x+" is odd"
    }
  
    // the method orElse allows chaining another partial function to handle
    // input outside the declared domain
    val numbers = sample map (isEven orElse isOdd)
    numbers.foreach(println)
  }
}